// 路由
var express = require('express');
var router  = express.Router();

// api 请求中间件
// var multifetch = require('multifetch');
// router.get('/api/multifetch',function (req,req,next) {
// 	res.setHeader("Access-Control-Allow-Origin","*");
// 	res.setHeader("Access-Control-Allow-Methods","GET");
// 	res.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
// 	next();
// })

// 数据库查询
//var sqlQuery    = require('../lib/sqlQuery');
var queryArray  = require('../lib/queryArray');
var queryObject = require('../lib/queryObject');


var _ = require("underscore")._;
router.maxSockets = 200;

// var  events = require('events');
// var emitter = new events.EventEmitter();
// emitter.setMaxListeners(0);

// query 操作

// cors设置
router.get('*',function (req,res,next) {
	res.setHeader("Access-Control-Allow-Origin","*");
	res.setHeader("Access-Control-Allow-Methods","GET");
	res.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
	next();
});

router.get('/api/multi',function (req,res,next) {
	// resources = useres,dept,classes
	var sqllist = '';
	var multi = req.query.resources;

	sqllist = _.map( multi.split(',') ,function (resource) {
		return 'select * from ' + resource+';';
	}).join('');
	// console.log(sqllist);
	// res.end(sqllist)
	queryArray(sqllist,[],res);
});

router.get('/api/:resource',function (req, res, next){

	// res.setHeader("Access-Control-Allow-Origin","*");
	// res.setHeader("Access-Control-Allow-Methods","GET");
	// res.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
	var resourceName = req.params.resource,
	    param = req.query,
	    sql = '';
	if (_.isEmpty(param)){
		sql = 'SELECT * FROM '+resourceName;
	}else{
		sql = 'SELECT * FROM '+resourceName+' WHERE ?';
	}
	console.log('sql ==> ' + sql);
	queryArray(sql,param,res);
	// res.json({name:resourceName})
});

// 查询操作ById
router.get('/api/:resource/:id', function(req, res, next) {
	res.setHeader("Access-Control-Allow-Origin","*");
	res.setHeader("Access-Control-Allow-Methods","GET");
	res.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
  var resourceName = req.params.resource,
	    id = parseInt( req.params.id ),
	    param = req.query,
	    json;
	console.log('param ==>' + JSON.stringify(param))
	console.log('resourceName ==>' + resourceName);
	var sql = '';

	sql = 'SELECT * FROM '+resourceName+' WHERE id = ?';
	console.log(sql);
	queryObject(sql ,[id],res);
});
// save操作
router.post('/api/:resource',function(req, res, next){
	res.setHeader("Access-Control-Allow-Origin","*");
	res.setHeader("Access-Control-Allow-Methods","POST");
	res.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
	var resourceName = req.params.resource,
	    param        = req.body,
	    sql = 'insert into '+resourceName+' set ?';

	queryArray(sql,param,res);
});
// update操作
router.put('/api/:resource/:id',function(req,res,next){
	res.setHeader("Access-Control-Allow-Origin","*");
	res.setHeader("Access-Control-Allow-Methods","PUT");
	res.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
	var resourceName = req.params.resource,
	    id           = req.params.id,
	    params       = req.body,
	    sql          = '';
	// console.log('params');
	// console.log(JSON.stringify(req.query));
	// console.log(JSON.stringify(req.body));
	// console.log(JSON.stringify(req.params));
	sql = 'UPDATE '+resourceName+' SET ? where id = '+id+';'

	queryArray(sql,params,res);
});
// delete操作
router.delete('/api/:resource/:id',function(req,res,next){
	res.setHeader("Access-Control-Allow-Origin","*");
	res.setHeader("Access-Control-Allow-Methods","DELETE");
	res.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
	var resourceName = req.params.resource,
	    id           = req.params.id,
	    sql          = '';
	sql = 'delete from '+ resourceName +' where id = '+id;
	queryArray(sql,res);
});

router.get('/sqlUpdateStatement',function(req,res,next){
	var str = '';
	var students = require('./students.js');
	for(var i = 0 ;i < students.length ; i++){
		// update 表名称 set 列名称=新值 where 更新条件;
		var stu = students[i];
		str += 'UPDATE usr SET ';
		str += 'usr_num = "' + stu.student_id + '",';
		str += 'WHERE id = ' + stu.id + ';';
		str += '\n';
	}
	// console.log(str);
	res.setHeader('content-type','text/plain;charset=utf-8');
	res.write(str);
	res.end();
});

module.exports = router;
